import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUi from 'element-ui'
import Axios from 'axios'
import 'element-ui/lib/theme-chalk/index.css'

import utils from './common/js/utils.js'
import verify from './common/js/verify.js'
import mixin from '@/common/js/mixin.js'
import Filter from '@/common/js/filter.js'
import store from './store'


import SlideVerify from 'vue-monoplasty-slide-verify';
Vue.use(SlideVerify);


import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Object.keys(Filter).forEach(key => Vue.filter(key, Filter[key]))
Vue.mixin(mixin)

Vue.use(ElementUi)







if(location.href.indexOf('https')!=-1){
  Vue.prototype.$domain = 'https://hs.kxm.link/index.php'
  Vue.prototype.$baseUrl = 'https://hs.kxm.link'
  Vue.prototype.$env = 'prodect'
}else{
  Vue.prototype.$domain = 'http://www.hs.com/index.php'
  Vue.prototype.$baseUrl = 'http://www.hs.com'
   Vue.prototype.$env = 'dev'
}


// 拖拽
import VueDND from 'awe-dnd'
Vue.use(VueDND)


const obj = {
  utils,
  verify
}

for (const key in obj) {
  Object.defineProperty(Vue.prototype, `$${key}`, {
    get: () => obj[key]
  })
}
NProgress.configure({
easing: 'ease', // 动画方式
speed: 600, // 递增进度条的速度
showSpinner: true, // 是否显示加载ico
trickleSpeed: 200, // 自动递增间隔
minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to,from,next) => {
  document.title = to.meta.title || '数码回收'
  NProgress.start() 
  next()
})
router.afterEach(() => {
  NProgress.done()
})


Vue.config.productionTip = false



Vue.prototype.axios = Axios;


Vue.prototype.$post = function (url, data, callback) {
    var _this = this
    let cid = sessionStorage.getItem('cid')
	let token = sessionStorage.getItem('token')
	let user_name = sessionStorage.getItem('user_name')
	
	if(!cid){
		ElementUi.Message({
		  message:'请重新登录',
		  type:'error'
		})
		return this.$router.push('/')
	}
	
    data.cid=cid
	data.user_name=user_name
	//去除筛选参数中的空格
	if(data.search_form){
		for(let i in data.search_form){
			if(i != 'goods_name'){
				data.search_form[i] = data.search_form[i].replace(/ /g,'');
			}
		}
	}
	
    if (url.indexOf('http') == -1) {
        url = this.$domain + url
    }
	const loading = this.$loading({
	  text: '加载中',
	  background: 'rgba(255, 255, 255, 0.1)',
	  spinner: 'el-icon-loading',
	  customClass: 'loadingclass'
	})
	token = token+'_'+cid
     _this.axios.post(url, data,{
		 headers: {
		   Authorization:token
	     }
	 }).then(function (res) {
        var call = callback || function (res) {}
        if(res.data.code==401){
          _this.$router.push('/')
		  ElementUi.Message({
			  message:'请从新登录',
			  type:'error'
		  })
        }else if(res.data.code==400){
          ElementUi.Message({
			  message:res.data.msg,
			  type:'error'
		  })
        }else{
			 call(res)
		}
		 loading.close()
     })
}

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
